package com.heimali.sf.fragments;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.heimali.sf.R;

/**
 * 申请费
 */
public class AppFeeFragment extends BaseFragment {
	public static final String TAG = "AppFeeFragment";

	private EditText mInputEditText;
	private TextView mExecuteFeeText;
	private TextView mPreserveFeeText;
	private TextView mPaymentFeeText;
	private TextView mTipText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_app_fee, container, false);
		mExecuteFeeText = (TextView) v.findViewById(R.id.execute_fee_amount);
		mPreserveFeeText = (TextView) v.findViewById(R.id.preserve_fee_amount);
		mPaymentFeeText = (TextView) v.findViewById(R.id.payment_fee_amount);

		mTipText = (TextView) v.findViewById(R.id.tip);

		mInputEditText = (EditText) v.findViewById(R.id.money_input);
		mInputEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				calcFee();
			}
		});
		initFeeText();
		return v;
	}

	@Override
	public void onResume() {
		super.onResume();
		refreshUI();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	public void refreshUI() {
		calcFee();
		mTipText.setText(getString(R.string.app_fee_tip, "50.0", "30.0"));
	}

	private void initFeeText() {
		calcFee();
	}

	private void calcFee() {
		String inputStr = mInputEditText.getText().toString();
		double input = TextUtils.isEmpty(inputStr) ? 0 : Double.valueOf(inputStr);
		mExecuteFeeText.setText(String.valueOf(formatF2(calcExecute(input))));
		mPreserveFeeText.setText(String.valueOf(formatF2(calcPreserve(input))));
		mPaymentFeeText.setText(String.valueOf(formatF2(calcPayment(input))));
	}

	protected double calcExecuteOdd(double sum) {
		double r = 0;
		if (sum <= 10000) {
			r = 50 * 100;
		} else if (sum <= 500000) {
			r = (sum - 10000) * 1.5;
		} else if (sum <= 5000000) {
			r = (sum - 500000) * 1;
		} else if (sum <= 10000000) {
			r = (sum - 5000000) * 0.5;
		} else {
			r = (sum - 10000000) * 0.1;
		}
		return r / 100;
	}

	/**
	 * 执行案件
	 * 
	 * @param sum
	 * @return
	 */
	private double calcExecute(double sum) {
		double L0 = calcExecuteOdd(10000), L1 = L0 + calcExecuteOdd(500000), L2 = L1 + calcExecuteOdd(5000000), L3 = L2
				+ calcExecuteOdd(10000000);

		if (sum <= 10000) {
			return L0;
		} else if (sum <= 500000) {
			return L0 + calcExecuteOdd(sum);
		} else if (sum <= 5000000) {
			return L1 + calcExecuteOdd(sum);
		} else if (sum <= 10000000) {
			return L2 + calcExecuteOdd(sum);
		} else {
			return L3 + calcExecuteOdd(sum);
		}

	}

	private double calcPreserveOdd(double sum) {
		double r = 0;
		if (sum <= 1000) {
			r = 30 * 100;
		} else if (sum <= 100000) {
			r = (sum - 1000) * 1;
		} else if (sum > 100000) {
			r = (sum - 100000) * 0.5;
		}
		return r / 100;
	}

	/**
	 * 财产保全
	 * 
	 * @param sum
	 * @return
	 */
	private double calcPreserve(double sum) {
		double L0 = 30, L1 = L0 + calcPreserveOdd(100000);

		if (sum <= 1000) {
			return L0;
		} else if (sum <= 100000) {
			return L0 + calcPreserveOdd(sum);
		} else {
			return Math.min(5000, L1 + calcPreserveOdd(sum));
		}
	}

	/**
	 * 执行令
	 * 
	 * @param sum
	 * @return
	 */
	private double calcPayment(double sum) {
		return calcProperty(sum) * rebate / 3;
	}
}
